﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;

namespace ZHDJ.AppInterface.Controllers
{
    public class PhotoWallController : ApiController
    {
        #region 获取留影墙数据
        [HttpPost]
        public ReturnResult GetPhotoWall(dynamic obj)
        {
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string CreateBy = Convert.ToString(obj.CreateBy);
            DataTable dt = InterfaceHelper.GetPhotoWall(pageIndex, pageSize, CreateBy);
            IList<PartyPhotoWall> list = new List<PartyPhotoWall>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<PartyPhotoWall>.ConvertToModel(dt);

            foreach (PartyPhotoWall model in list)
            {
                DataTable dtfile = InterfaceHelper.GetPartyPhotoWallImageByPhotoWallID(model.ID);
                List<string> newlist = new List<string>();
                foreach (DataRow item in dtfile.Rows)
                {
                    newlist.Add(item["Url"].ToString());
                }
                model.PicList = newlist;
            }

            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion

        #region 获取留影墙实体
        [HttpPost]
        public ReturnResult GetPhotoWallModel(dynamic obj)
        {
            string ID = Convert.ToString(obj.ID);
            PartyPhotoWall model = InterfaceHelper.GetPhotoWallModel(ID);
            return new ReturnResult
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion

        #region 留影墙保存方法
        [HttpPost]
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult result = new ReturnResult();
            try
            {
                PartyPhotoWall model = new PartyPhotoWall();
                model.ID = StringHelper.NewID;
                model.Title = Convert.ToString(obj.Title);
                model.BranchID = Convert.ToString(obj.BranchID);
                model.Content = "";
                model.Category = Convert.ToString(obj.Category);
                model.Thematic = Convert.ToString(obj.Thematic);
                model.Status = 4;
                model.Aduit = 2;
                model.Flags = 0;
                model.SourceId = "";
                model.IsPush = false;
                model.CreateBy = Convert.ToString(obj.CreateBy);
                model.CreateTime = DateTime.Now;
                model.ModifyBy = Convert.ToString(obj.ModifyBy);
                model.ModifyTime = DateTime.Now;
                InterfaceHelper.AddPartyPhotoWall(model);
                
                //批量添加文件表
                string[] array = Convert.ToString(obj.imgString).TrimEnd(',').Split(',');
                for (int i = 0; i < array.Length; i++)
                {
                    PartyFile file = new PartyFile();
                    file.ID = StringHelper.NewID;
                    file.FID = "";
                    file.Title = model.Title + i.ToString();
                    file.Extension = ".jpg";
                    file.Description = model.Title + i.ToString();
                    file.Size = 0;
                    file.Url = array[i];
                    file.CreateTime = DateTime.Now;
                    file.CreateBy = model.CreateBy;
                    file.ModifyTime = DateTime.Now;
                    file.ModifyBy = model.CreateBy;
                    InterfaceHelper.AddFileTalbe(file);

                    PartyPhotoWallImage photo = new PartyPhotoWallImage();
                    photo.ID = StringHelper.NewID;
                    photo.PhotoWallID = model.ID;
                    photo.FileID = file.ID;
                    photo.Describe = model.Title;
                    photo.Status = 0;
                    photo.OrderNo = 0;
                    photo.CreateBy = model.CreateBy;
                    photo.CreateTime = DateTime.Now;
                    photo.ModifyBy = model.CreateBy;
                    photo.ModifyTime = DateTime.Now;
                    InterfaceHelper.AddPartyPhotoWallImage(photo);
                }
                result.code = "200";
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.code = "201";
                result.msg = "保存失败！";
            }
            return result;
        }
        #endregion

        #region 根据字典类型获取字典数据
        [HttpPost]
        public ReturnResult GetDictionaryByDDicType(dynamic obj)
        {
            string DDicType = Convert.ToString(obj.DDicType);
            DataTable dt = InterfaceHelper.GetDictionaryByDDicType(DDicType);
            IList<DataDictionary> list = new List<DataDictionary>();
            if (dt.Rows.Count > 0)
                list = DataTableToListModel<DataDictionary>.ConvertToModel(dt);

            return new ReturnResult
            {
                code = "200",
                result = list,
                msg = "success"
            };
        }
        #endregion
    }
}
